package io.gitee.yxsnake.framework.config.jasypt.encrypt;

import io.gitee.yxsnake.framework.core.exception.ServiceException;
import io.gitee.yxsnake.framework.core.utils.sm.Sm4Utils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;

/**
 * @author: snake
 * @create-time: 2024-10-22
 * @description:
 * @version: 1.0
 */
@Slf4j
@RequiredArgsConstructor
public class Sm4Encryptor implements StringEncryptor {

    private final String sm4Key;

    @Override
    public String encrypt(String s) {
        String encrypt = null;
        try {
            encrypt = Sm4Utils.encrypt(sm4Key, s);
        } catch (Exception e) {
            log.error("加密失败:",e);
            throw new ServiceException("加密失败",400);
        }
        return encrypt;
    }

    @Override
    public String decrypt(String s) {
        try {
            return Sm4Utils.decrypt(sm4Key,s);
        } catch (Exception e) {
            log.error("解密失败:",e);
            throw new ServiceException("解密失败",400);
        }
    }
}